package com.ruiyun.app.lib.wxshare

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler

/**
 * Author: wcy
 * Date: 2019-02-14 10:31
 * 重庆锐云科技有限公司
 */
abstract class WxBaseActivity : AppCompatActivity(), IWXAPIEventHandler {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WxShareManager.getInstance().getApi().handleIntent(intent, this)
    }


    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        WxShareManager.getInstance().getApi().handleIntent(intent, this)
    }

    /**
     * 分享后回调
     * */
    override fun onResp(p0: BaseResp?) {
        var resp = ShareResp()
        resp.code = p0!!.errCode
        when (p0!!.errCode) {
            BaseResp.ErrCode.ERR_OK -> {
                var code = ""
                try {
                    code = (p0 as SendAuth.Resp).code
                    resp.value = code;
                } catch (e: Exception) {

                }
                toast("分享成功", resp)
            }
            BaseResp.ErrCode.ERR_USER_CANCEL -> {
                toast("分享取消", resp)
            }
            BaseResp.ErrCode.ERR_AUTH_DENIED -> {
                toast("分享被拒绝", resp)
            }
            else -> {
                toast("分享返回", resp)
            }
        }
        finish()
    }

    fun toast(tag: String, resp: ShareResp) {
        Log.d("ddddddddd", "=====>${tag}")
        resp.msg = tag
        onSucceed(resp)
    }
    abstract  fun onSucceed(resp: ShareResp)
    override fun onReq(p0: BaseReq?) {

    }

}